<template>
  <el-dialog :title="formValidate.id ? $t('letterReviewMember.groupEdit') : $t('letterReviewMember.groupAdd')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('letterReviewMember.searchModel.companyName')" prop="firmName">
        <el-input v-model="formValidate.firmName" clearable :placeholder="$t('letterReviewMember.prompt.companyName')" :maxlength="100" />
      </el-form-item>
      <el-form-item :label="$t('letterReviewMember.model.companyIntroduction')">
        <el-input
          v-model="formValidate.firmIntroduction"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 6}"
          :placeholder="$t('letterReviewMember.prompt.companyIntroduction')"
          :maxlength="500"
          show-word-limit
        />
      </el-form-item>

      <el-form-item :label="$t('letterReviewMember.searchModel.companyType')" prop="firmType">
        <el-select v-model="formValidate.firmType" clearable :placeholder="$t('letterReviewMember.prompt.companyType')" style="width: 100%">
          <el-option
            v-for="item in $t('letterReviewMember.companyType')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('letterReviewMember.searchModel.status')" prop="status">
        <el-select v-model="formValidate.status" clearable :placeholder="$t('letterReviewMember.prompt.status')" style="width: 100%">
          <el-option
            v-for="item in $t('letterReviewMember.status')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateCreditReviewFirm, addCreditReviewFirm } from '@/api/risk'
export default {
  name: 'CompanyDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: {
        id: null,
        firmName: null,
        firmIntroduction: null,
        firmType: null,
        status: null,
        releaseCase: null
      },
      rules: {
        firmName: [{ required: true, message: this.$t('letterReviewMember.searchModel.companyName') + this.$t('common.null'), trigger: 'blur' }],
        firmType: [{ required: true, type: 'number', message: this.$t('letterReviewMember.searchModel.companyType') + this.$t('common.null'), trigger: 'blur' }],
        status: [{ required: true, type: 'number', message: this.$t('letterReviewMember.searchModel.status') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const formData = {
            id: this.formValidate.id,
            firmName: this.formValidate.firmName,
            firmIntroduction: this.formValidate.firmIntroduction,
            firmType: this.formValidate.firmType,
            status: this.formValidate.status,
            releaseCase: this.formValidate.status === 2 ? 2 : null
          }
          if (formData.id) {
            updateCreditReviewFirm(formData)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addCreditReviewFirm(formData)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
